package xdu.lz.aop.xmltest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import org.springframework.stereotype.Component;


@Aspect
@Component
public class MyAOP_AspectJ {
	@Pointcut("execution(* xdu.lz.aop.xmltest.Car.*(..))")
	public void myCutPoint() {

	}

	@Before("myCutPoint()")
	public void myBefore(JoinPoint jp) {
		//jp是织入点对象:指代目标织入方法
		System.out.println("2前置通知..." + jp.getSignature().getName());
	}

	@After("myCutPoint()")
	public void myAfter(JoinPoint jp) {
		System.out.println("4后置通知...始终会被执行..." + jp.getSignature().getName());
	}

	@AfterReturning(pointcut = "myCutPoint()", returning = "returnValue")
	public void myAfterReturning(JoinPoint jp, Object returnValue) {
		System.out.print("5返回后通知...退出around后执行" + jp.getSignature().getName());
		System.out.println("返回值:" + returnValue);
	}

	/**
	 * 环绕通知的功能等价于前置+执行+后置。重点掌握环绕通知即可
	 */
	@Around("myCutPoint()")
	public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("1环绕通知-前-开启事务...");
		//放行
		Object returnValue = pjp.proceed();
		System.out.println("3环绕通知-后-关闭事物...");
		System.out.println(11111);
		return returnValue;
	}

	@AfterThrowing(pointcut = "myCutPoint()", throwing = "e")
	public void myThrowing(JoinPoint jp, Throwable e) {
		System.out.println("异常..." + jp.getSignature().getName() + " 异常" + e.getMessage());
	}

}
